package im.yangqiang.android.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导图控件
 * Created by Carlton on 2016/3/30.
 */
public class BaseGuideView extends View
{
    private List<FocusData> mLists;
    private Paint           mPaint;
    private boolean isDebug        = false;
    /**
     * 点击是否消失
     */
    private boolean isTouchOutside = true;
    /**
     * 进入动画
     */
    private Animation mEnterAnimation;
    /**
     * 退出动画
     */
    private Animation mExitAnimation;
    /**
     * 背景的透明度
     */
    private byte mAlpha = 100;
    private List<Bitmap> mBitmaps;
    private List<PointF> mPoints;

    public BaseGuideView(Context context)
    {
        super(context);
        init(context, null, -1);
    }

    public BaseGuideView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context, attrs, -1);
    }

    public BaseGuideView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr)
    {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mBitmaps = new ArrayList<>();
        mPoints = new ArrayList<>();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_UP:
                onDownClick(event);
                break;
        }
        return true;
    }

    private void onDownClick(MotionEvent event)
    {
        if (isTouchOutside)
        {
            dismiss();
        }
    }

    /**
     * 设置点击的时候是否消失
     *
     * @param touchOutside
     */
    public void setTouchOutside(boolean touchOutside)
    {
        isTouchOutside = touchOutside;
    }

    /**
     * 显示
     */
    public void show()
    {
        if (null == mEnterAnimation)
        {
            setVisibility(VISIBLE);
            return;
        }
        startAnimation(mEnterAnimation);
    }

    /**
     * 消失
     */
    public void dismiss()
    {
        if (null == mExitAnimation)
        {
            setVisibility(GONE);
            return;
        }
        startAnimation(mExitAnimation);
    }

    /**
     * 设置显示和消失的动画
     *
     * @param enter
     * @param exit
     */
    public void setAnimation(Animation enter, Animation exit)
    {
        mEnterAnimation = enter;
        mExitAnimation = exit;
    }

    /**
     * 是否启用调式
     *
     * @return
     */
    public boolean isDebug()
    {
        return isDebug;
    }

    /**
     * 开关调试模式
     *
     * @param debug
     */
    public void setDebug(boolean debug)
    {
        isDebug = debug;
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<FocusData> list)
    {
        mLists = list;
    }

    /**
     * 刷新
     */
    public void refresh()
    {
        requestLayout();
        invalidate();
    }

    /**
     * 设置背景透明度，值为0...255
     *
     * @param alpha
     */
    public void setBackgroundAlpha(int alpha)
    {
        mAlpha = (byte) alpha;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        if (null == mLists)
        {
            return;
        }
        canvas.save();
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawARGB(mAlpha, 0, 0, 0);
        onDrawOther(canvas);
        for (FocusData data : mLists)
        {
            Matrix matrix = new Matrix();
            matrix.postScale(data.scaleRatio, data.scaleRatio);
            matrix.postTranslate(data.getBound().centerX() - (data.getBound().width() * data.getScaleRatio()) / 2, data.getBound().centerY() - (data.getBound
                    ().height() * data.getScaleRatio()) / 2);
            canvas.drawBitmap(data.getBitmap(), matrix, mPaint);
            if (isDebug)
            {
                drawDebug(canvas, data.getBound().left, data.getBound().top, data.getBound().right, data.getBound().bottom);
            }
        }
        if (null == mBitmaps || null == mPoints || mBitmaps.size() != mPoints.size())
        {
            return;
        }
        int size = mBitmaps.size();
        for (int i = 0; i < size; ++i)
        {
            Bitmap bitmap = mBitmaps.get(i);
            PointF point = mPoints.get(i);
            float left = point.x - bitmap.getWidth() / 2;
            float top = point.y - bitmap.getHeight() / 2;
            canvas.drawBitmap(bitmap, left, top, mPaint);
            if (isDebug)
            {
                drawDebug(canvas, left, top, left + bitmap.getWidth(), top + bitmap.getHeight());
            }
        }
        canvas.restore();
    }

    /**
     * 绘制debug模式的红色边框
     *
     * @param canvas
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    protected void drawDebug(Canvas canvas, float left, float top, float right, float bottom)
    {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAlpha(80);
        canvas.drawRect(left, top, right, bottom, paint);
    }

    /**
     * 返回所有焦点数据
     *
     * @return
     */
    public List<FocusData> getLists()
    {
        return mLists;
    }

    /**
     * 绘制其他图层
     *
     * @param canvas
     */
    protected void onDrawOther(Canvas canvas)
    {

    }

    /**
     * 添加绘制图片
     *
     * @param bitmap
     * @param center
     */
    public void addBitmap(Bitmap bitmap, PointF center)
    {
        mBitmaps.add(bitmap);
        mPoints.add(center);
    }

    /**
     * 焦点数据
     */
    public static class FocusData
    {
        /**
         * 焦点图片的边框,这个位置很重要，是图片在屏幕上的相对于该控件以左上角为0开始的位置，right和bottom属性必须是left+图片的宽和top+图片高，而不是直接是图片的宽高
         */
        private RectF  bound      = new RectF();
        /**
         * 焦点图片的图片资源
         */
        private Bitmap bitmap     = null;
        /**
         * 焦点图片的缩放比例
         */
        private float  scaleRatio = 1;

        public FocusData()
        {

        }

        public FocusData(RectF bound, Bitmap bitmap, float scaleRatio)
        {
            this.bound = bound;
            this.bitmap = bitmap;
            this.scaleRatio = scaleRatio;
        }

        public RectF getBound()
        {
            return bound;
        }

        public void setBound(RectF bound)
        {
            this.bound = bound;
        }

        public Bitmap getBitmap()
        {
            return bitmap;
        }

        public void setBitmap(Bitmap bitmap)
        {
            this.bitmap = bitmap;
        }

        public float getScaleRatio()
        {
            return scaleRatio;
        }

        public void setScaleRatio(float scaleRatio)
        {
            this.scaleRatio = scaleRatio;
        }
    }
}
